﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.Geometry;
using Microsoft.Graphics.Canvas.Text;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Printing.Workflow;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using 纸片人.AttributeUsageFloder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{

    [LastModified("2022-5-12", "create ShapePage.xaml")]
    [PageType("cpu", "形状变形")]

    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class ShapePage : Page
    {




        Side tSide, bSide, lSide, rSide;


        int width;
        int height;
        Color[] colors;
        Color[] tempColors;
        WriteableBitmap wb;

        CanvasBitmap cb;



        List<Bessel> listBessel = new List<Bessel>();

        Vector2 downPoint = new Vector2();

        Vector2 movePoint = new Vector2();

        Bessel previewLine = new Bessel();

        bool isDown = false;
        bool isPreview = false;
        bool isComplate = false;
        bool isClosure = false;
        Bessel selectPoint;
        public ShapePage()
        {
            this.InitializeComponent();







        }



        private async void selectPicture_Click(object sender, RoutedEventArgs e)
        {
            CanvasBitmap bitmap = await Function.SelectImage(canvas);

            if (bitmap == null)
            {
                return;
            }
            cb = CanvasBitmap.CreateFromDirect3D11Surface(canvas, bitmap);

            Function.InitialParameter(bitmap, out width, out height, out colors, out tempColors, oriImage, out wb);

        }

        /// <summary>
        /// 画
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void canvas_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args)
        {
            using (var ds = args.DrawingSession)
            {
                args.DrawingSession.Clear(Colors.Gray);




                // 画辅助先
                #region
                double size = 50;
                double wm = canvas.ActualWidth / size;
                double hm = canvas.ActualHeight / size;

                for (int x = 0; x < wm; x++)
                {
                    for (int y = 0; y < hm; y++)
                    {
                        if (x % 2 != 0)
                        {
                            if (y % 2 != 0)
                            {
                                ds.FillRectangle(new Rect(x * size, y * size, size, size), Color.FromArgb(255, 219, 219, 219));

                            }
                            else
                            {
                                ds.FillRectangle(new Rect(x * size, y * size, size, size), Colors.White);

                            }
                        }
                        else
                        {
                            if (y % 2 != 0)
                            {
                                ds.FillRectangle(new Rect(x * size, y * size, size, size), Colors.White);

                            }
                            else
                            {
                                ds.FillRectangle(new Rect(x * size, y * size, size, size), Color.FromArgb(255, 219, 219, 219));

                            }
                        }



                    }
                }

                for (int x = 0; x < wm; x++)
                {
                    ds.DrawText((x * size).ToString(), new Vector2((float)(x * size), (float)(canvas.ActualHeight - 40)), Colors.Black);
                }

                for (int x = 0; x < hm; x++)
                {
                    ds.DrawText((x * size).ToString(), new Vector2((float)(canvas.ActualWidth - 40), (float)(x*size)), Colors.Black);
                }

                #endregion


                if (listBessel.Count > 0)
                {



                    CanvasPathBuilder path = new CanvasPathBuilder(sender);
                    CanvasPathBuilder line = new CanvasPathBuilder(sender);
                    Vector2 startPoint = listBessel[0].Vect;
                    line.BeginFigure(startPoint);
                    Vector2 previousVect = startPoint;
                    foreach (var item in listBessel)
                    {

                        line.AddCubicBezier(startPoint, item.LeftVect, item.Vect);
                        ds.FillRectangle(new Rect(item.Vect.X - 5, item.Vect.Y - 5, 10, 10), Colors.OrangeRed);
                        startPoint = item.RightVect;
                        previousVect = item.Vect;

                        string str =Math.Round( item.Vect.X) + "//" +Math.Round( item.Vect.Y);
                        CanvasTextFormat format = new CanvasTextFormat();
                        format.FontSize = 10f;
                        ds.DrawText(str, item.Vect, Colors.Green,format);


                    }

                    if (isPreview && !isComplate)
                    {
                        path.BeginFigure(previousVect);
                        path.AddCubicBezier(startPoint, previewLine.LeftVect, previewLine.Vect);
                        ds.DrawLine(previewLine.LeftVect, previewLine.RightVect, Colors.Blue);
                        path.EndFigure(CanvasFigureLoop.Open);



                    }
                    else
                    {
                        if (!isComplate)
                        {
                            Vector2 endPoint = listBessel.Last().LeftVect;
                            ds.DrawLine(endPoint, startPoint, Colors.Blue);
                        }


                    }



                    line.EndFigure(CanvasFigureLoop.Open);
                    CanvasGeometry apple = CanvasGeometry.CreatePath(path);

                    CanvasGeometry lineGemetry = CanvasGeometry.CreatePath(line);


                    args.DrawingSession.DrawGeometry(apple, Colors.Gray);
                    args.DrawingSession.DrawGeometry(lineGemetry, Colors.Black);
                }
            }

        }

        /// <summary>
        /// 点击画板，画点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(canvas).Position;

            downPoint.X = (float)point.X;
            downPoint.Y = (float)point.Y;

            if (isComplate)
            {
                int i = 0;

                foreach (var item in listBessel)
                {
                    if (downPoint.X > item.Vect.X - 5 && downPoint.X < item.Vect.X + 5 && downPoint.Y > item.Vect.Y - 5 && downPoint.Y < item.Vect.Y + 5)
                    {
                        selectPoint = item;

                        if (selectPoint == listBessel[0])
                        {
                            if (isClosure)
                            {
                                Bessel lastB = listBessel[listBessel.Count - 1];

                                VisibleDragThumb(lastB.LeftVect, selectPoint.Vect, selectPoint.RightVect, true, true);

                            }
                            else
                            {
                                VisibleDragThumb(selectPoint.LeftVect, selectPoint.Vect, selectPoint.RightVect, false, true);

                            }
                        }
                        else
                        {
                            VisibleDragThumb(selectPoint.LeftVect, selectPoint.Vect, selectPoint.RightVect, true, true);

                        }


                        return;
                    }

                    i++;
                }

                CollapsedDragThumb();

            }
            else
            {



                if (listBessel.Count > 1)
                {
                    Vector2 firstPoint = listBessel[0].Vect;
                    if (downPoint.X > firstPoint.X - 5 && downPoint.X < firstPoint.X + 5 && downPoint.Y > firstPoint.Y - 5 && downPoint.Y < firstPoint.Y + 5)
                    {
                        downPoint.X = firstPoint.X;
                        downPoint.Y = firstPoint.Y;
                        isClosure = true;
                    }


                }
                isDown = true;
            }
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(canvas).Position;

            movePoint.X = (float)point.X;
            movePoint.Y = (float)point.Y;

            if (isDown)
            {
                isPreview = true;
                SetPreviewLine(downPoint, movePoint);
                canvas.Invalidate();
            }
            
        }

        /// <summary>
        /// 鼠标弹起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            if (isComplate)
            {

            }
            else
            {
                if (listBessel.Count > 1)
                {
                    Vector2 firstPoint = listBessel[0].Vect;
                    if (downPoint.X == firstPoint.X && downPoint.Y == firstPoint.Y)
                    {
                        isComplate = true;
                    }


                }

                isDown = false;
                isPreview = false;
                Bessel bessel = new Bessel();

                if (listBessel.Count == 0)
                {
                    bessel.LeftVect = downPoint;

                }
                else
                {
                    bessel.LeftVect = CalculationLeftPoint(downPoint, movePoint);
                }

                bessel.Vect = downPoint;
                bessel.RightVect = movePoint;
                listBessel.Add(bessel);
                canvas.Invalidate();

            }



        }

        void SetPreviewLine(Vector2 vect, Vector2 rightVect)
        {
            previewLine.Vect = vect;
            previewLine.RightVect = rightVect;
            previewLine.LeftVect = CalculationLeftPoint(vect, rightVect);
        }

        /// <summary>
        /// 清除画板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearPath_Click(object sender, RoutedEventArgs e)
        {

            listBessel.Clear();
            canvas.Invalidate();
            isComplate = false;
            isClosure = false;

            CollapsedDragThumb();

            canvas.Invalidate();
        }


   



       async void HorizontalAndVertical()
        {
            if (isClosure)
            {
                List<DeformSide> listSide = new List<DeformSide>();

                for (int i = 0; i <= listBessel.Count - 2; i++)
                {
                    var cBessel = listBessel[i];
                    var nBessel = listBessel[i + 1];
                    var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                    ds.GetBezierLengthAndPoint();
                    listSide.Add(ds);
                }

                double l = listSide.OrderBy(p => p.Left.X).First().Left.X;
                double r = listSide.OrderBy(p => p.Right.X).Last().Right.X;
                double t = listSide.OrderBy(p => p.Top.Y).First().Top.Y;
                double b = listSide.OrderBy(p => p.Top.Y).Last().Bottom.Y;

               

                //图形的长和宽
                int shapew = (int)(r - l);
                int shapeh = (int)(b - t);

               




                List<Bessel> cacheListBessel = new List<Bessel>();

                foreach (var item in listBessel)
                {
                    Bessel be = new Bessel();
                    be.Vect = new Vector2(item.Vect.X - (float)l, item.Vect.Y - (float)t);
                    be.RightVect = new Vector2(item.RightVect.X - (float)l, item.RightVect.Y - (float)t);
                    be.LeftVect = new Vector2(item.LeftVect.X - (float)l, item.LeftVect.Y - (float)t);
                    cacheListBessel.Add(be);
                }

                if (scaleToggle.IsChecked == true)
                {
                    double scale = 1;
                    if (shapew < width)
                    {
                        scale = (double)width / (double)shapew;
                    }
                    else if (shapeh < height)
                    {
                        scale = (double)height / (double)shapeh;

                    }

                    foreach (var item in cacheListBessel)
                    {
                        item.Vect = new Vector2((float)(item.Vect.X * scale), (float)(item.Vect.Y * scale));
                        item.RightVect = new Vector2((float)(item.RightVect.X * scale), (float)(item.RightVect.Y * scale));
                        item.LeftVect = new Vector2((float)(item.LeftVect.X * scale), (float)(item.LeftVect.Y * scale));
                    }
                }
                int ltIndex = 0, rtIndex = 0, lbIndex = 0, rbIndex = 0;

                Vector2 ltxy, rtxy, lbxy, rbxy;
                ltxy = rtxy = lbxy = rbxy = listBessel[0].Vect;

                foreach (var item in listBessel)
                {
                    if (item == listBessel.Last())
                        break;

                    var temp = item.Vect;

                    if (Math.Pow(temp.X - l, 2) + Math.Pow(temp.Y - t, 2) < Math.Pow(ltxy.X - l, 2) + Math.Pow(ltxy.Y - t, 2))
                    {
                        ltIndex = listBessel.IndexOf(item);
                        ltxy = temp;
                    }

                    if (Math.Pow(temp.X - r, 2) + Math.Pow(temp.Y - t, 2) < Math.Pow(rtxy.X - r, 2) + Math.Pow(rtxy.Y - t, 2))
                    {
                        rtIndex = listBessel.IndexOf(item);
                        rtxy = temp;

                    }

                    if (Math.Pow(temp.X - l, 2) + Math.Pow(temp.Y - b, 2) < Math.Pow(lbxy.X - l, 2) + Math.Pow(lbxy.Y - b, 2))
                    {
                        lbIndex = listBessel.IndexOf(item);
                        lbxy = temp;

                    }

                    if (Math.Pow(temp.X - r, 2) + Math.Pow(temp.Y - b, 2) < Math.Pow(rbxy.X - r, 2) + Math.Pow(rbxy.Y - b, 2))
                    {
                        rbIndex = listBessel.IndexOf(item);
                        rbxy = temp;

                    }
                }

                List<DeformSide> lv = new List<DeformSide>();

                tSide = new Side();
                rSide = new Side();
                lSide = new Side();
                bSide = new Side();

                //左上角最大
                if (ltIndex >= rtIndex && ltIndex >= lbIndex && ltIndex >= rbIndex)
                {

                    //逆时针
                    if (rtIndex>lbIndex  )
                    {
                        //上边边
                        for (int i = ltIndex; i > rtIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i < cacheListBessel.Count - 1; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }



                        for (int i = 0; i < lbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        lSide.BesselList.AddRange(lv);
                        lv.Clear();


                    }
                    else
                    {
                        //上边边
                        for (int i = ltIndex; i < cacheListBessel.Count - 1; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        for (int i = 0; i < rtIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i > lbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        lSide.BesselList.AddRange(lv);
                        lv.Clear();




                    }
                }
                //右上角最大
                else if (rtIndex > ltIndex && rtIndex > lbIndex && rtIndex > rbIndex)
                {

                    //逆时针
                    if (rbIndex > lbIndex)
                    {
                        //上边边
                        for (int i = ltIndex; i > 0; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        for (int i = cacheListBessel.Count - 1; i > rtIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i < lbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        lSide.BesselList.AddRange(lv);
                        lv.Clear();


                    }
                    else
                    {
                        //上边边
                        for (int i = ltIndex; i < rtIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i < cacheListBessel.Count - 1; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        for (int i = 0; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i > lbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        lSide.BesselList.AddRange(lv);
                        lv.Clear();




                    }
                }

                //右下角最大
                else if (rbIndex > ltIndex && rbIndex > lbIndex && rbIndex > rtIndex)
                {

                    //逆时针
                    if (lbIndex > ltIndex)
                    {
                        //上边边
                        for (int i = ltIndex; i > rtIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i > 0; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        for (int i = cacheListBessel.Count - 1; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i < lbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }




                        lSide.BesselList.AddRange(lv);
                        lv.Clear();


                    }
                    else
                    {
                        //上边边
                        for (int i = ltIndex; i < rtIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i > 0; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        for (int i = cacheListBessel.Count - 1; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i > lbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }
                        lSide.BesselList.AddRange(lv);
                        lv.Clear();




                    }
                }

                //左下角最大
                else if (lbIndex > ltIndex && lbIndex > rbIndex && lbIndex > rtIndex)
                {

                    //逆时针
                    if (ltIndex > rbIndex)
                    {
                        //上边边
                        for (int i = ltIndex; i > rtIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }


                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i < cacheListBessel.Count - 1; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        for (int i = 0; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i < lbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }




                        lSide.BesselList.AddRange(lv);
                        lv.Clear();


                    }
                    else
                    {
                        //上边边
                        for (int i = ltIndex; i < rtIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            tSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        tSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //右边边
                        for (int i = rtIndex; i < rbIndex; i++)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i + 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.RightVect, nBessel.LeftVect, nBessel.Vect);
                            rSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        rSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //下边边
                        for (int i = lbIndex; i > rbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            bSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }



                        bSide.BesselList.AddRange(lv);
                        lv.Clear();

                        //左边边
                        for (int i = ltIndex; i > 0; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        for (int i = cacheListBessel.Count - 1; i > lbIndex; i--)
                        {
                            var cBessel = cacheListBessel[i];
                            var nBessel = cacheListBessel[i - 1];
                            var ds = new DeformSide(cBessel.Vect, cBessel.LeftVect, nBessel.RightVect, nBessel.Vect);
                            lSide.Length += ds.GetBezierLength();
                            lv.Add(ds);
                        }

                        lSide.BesselList.AddRange(lv);
                        lv.Clear();




                    }
                }

                double sideWidth = Math.Max(tSide.Length, bSide.Length);
                double sideHeight = Math.Max(lSide.Length, rSide.Length);

                

                Point[,] horizontalMatric = new Point[(int)sideWidth, 2];
                Point[,] verticalMatric = new Point[(int)sideHeight, 2];

                t = 9999999;
                b = 0;
                r = 0;
                l = 9999999;

                for (double x = 0; x < (int)sideWidth; x++)
                {
                 

                   double xm = x / sideWidth;

                    double ctx = tSide.Length * xm;
                    foreach (var item in tSide.BesselList)
                    {
                        if (item.BesselLength > ctx)
                        {

                            Point point = item.CalculateBezierPointForCubic(ctx / item.BesselLength);
                            if (point.Y < t)
                            {
                                t = point.Y;
                            }
                            else if (point.Y > b)
                            {
                                b = point.Y;
                            }

                            if (point.X < l)
                            {
                                l = point.X;
                            }
                            else if( point.X> r)
                            {
                                r = point.X;
                            }


                            horizontalMatric[(int)x, 0] = point;
                                
                            break;
                        }
                        else
                        {
                            ctx -= item.BesselLength;
                        }
                    }

                    double cbx = bSide.Length * xm;
                    foreach (var item in bSide.BesselList)
                    {
                        if (item.BesselLength > cbx)
                        {
                            Point point = item.CalculateBezierPointForCubic(cbx / item.BesselLength);

                            if (point.Y < t)
                            {
                                t = point.Y;
                            }
                            else if (point.Y > b)
                            {
                                b = point.Y;
                            }

                            if (point.X < l)
                            {
                                l = point.X;
                            }
                            else if (point.X > r)
                            {
                                r = point.X;
                            }
                            horizontalMatric[(int)x, 1] = point;
                            break;
                        }
                        else
                        {
                            cbx -= item.BesselLength;
                        }
                    }




                }


                for (double y = 0; y < (int)sideHeight; y++)
                {


                    double ym = y / sideHeight;

                    double cly = lSide.Length * ym;
                    foreach (var item in lSide.BesselList)
                    {
                        if (item.BesselLength > cly)
                        {

                            Point point = item.CalculateBezierPointForCubic(cly / item.BesselLength);

                            if (point.Y < t)
                            {
                                t = point.Y;
                            }
                            else if (point.Y > b)
                            {
                                b = point.Y;
                            }

                            if (point.X < l)
                            {
                                l = point.X;
                            }
                            else if (point.X > r)
                            {
                                r = point.X;
                            }

                            verticalMatric[(int)y, 0] = point;

                            break;
                        }
                        else
                        {
                            cly -= item.BesselLength;
                        }
                    }

                    double cry = rSide.Length * ym;
                    foreach (var item in rSide.BesselList)
                    {
                        if (item.BesselLength > cry)
                        {
                            Point point = item.CalculateBezierPointForCubic(cry / item.BesselLength);

                            if (point.Y < t)
                            {
                                t = point.Y;
                            }
                            else if (point.Y > b)
                            {
                                b = point.Y;
                            }

                            if (point.X < l)
                            {
                                l = point.X;
                            }
                            else if (point.X > r)
                            {
                                r = point.X;
                            }
                            verticalMatric[(int)y, 1] = point;

                           
                            break;
                        }
                        else
                        {
                            cry -= item.BesselLength;
                        }
                    }




                }

                //图形的长和宽
                 shapew = (int)Math.Round(r - l);
                 shapeh = (int)Math.Round((b - t));
                tempColors = new Color[shapew * shapeh];


                Point ltPoint = horizontalMatric[0, 0];
                Point rtPoint = horizontalMatric[(int)sideWidth - 1, 0];

                Point lbPoint = horizontalMatric[0, 1];
                Point rbPoint = horizontalMatric[(int)sideWidth - 1, 1];

                for (int x = 0; x < (int)sideWidth; x++)
                {
                    double mw = (double)x / sideWidth;

                    Point txy = horizontalMatric[x, 0];
                    Point bxy = horizontalMatric[x, 1];

                    for (int y = 0; y < (int)sideHeight; y++)
                    {
                        double mh =(double) y / (sideHeight);

                        Point lxy = verticalMatric[y, 0];
                        Point rxy = verticalMatric[y, 1];

                        double oy = 0, ox = 0;

                        oy = lxy.Y - (lxy.Y - rxy.Y) * mw;
                        ox = txy.X - (txy.X - bxy.X) * mh;

                        double ll = (lxy.X - (ltPoint.X - (ltPoint.X - lbPoint.X) * mh)) * (1 - mw);
                        double rr = (rxy.X - (rtPoint.X - (rtPoint.X - rbPoint.X) * mh)) * mw;
                        ox = ox + ll + rr;

                        double tt = (txy.Y - (ltPoint.Y - (ltPoint.Y - rtPoint.Y) * mw)) * (1 - mh);
                        double dd = (bxy.Y - (lbPoint.Y - (lbPoint.Y - rbPoint.Y) * mw)) * (mh);
                        oy = oy + tt + dd;

                        if (ox < 0 || ox > (int)shapew - 1 || oy < 0 || oy > (int)shapeh - 1)
                            continue;

                        try
                        {
                            Color c = Function.BiLinear(mw * width, mh * height, colors, (int)width, (int)height);
                            tempColors[(int)ox + (int)oy * (int)shapew] = c;
                        }
                        catch
                        {
                            Color c = Function.BiLinear(mw * width, mh * height, colors, (int)width, (int)height);
                            tempColors[(int)ox + (int)oy * (int)shapew] = c;
                        }

                   
                    }
                }

                tempColors = Function.RencentFillColor(tempColors, shapew, shapeh);
                tempColors = Function.RencentFillColor(tempColors, shapew, shapeh);

                Function.InitialImage(tempColors, shapew, shapeh, targetImage);
            }
            else
            {
                var message = new MessageDialog("图形未绘制完成.....请将图像绘制至闭合.");
                await message.ShowAsync();

            }


        }


        Vector2 CalculationLeftPoint(Vector2 vect, Vector2 rightVect)
        {
            Vector2 leftVect = new Vector2();


            leftVect.X = vect.X + (vect.X - rightVect.X);
            leftVect.Y = vect.Y + (vect.Y - rightVect.Y);

            return leftVect;
        }

        Vector2 CalculationRightPoint(Vector2 vect, Vector2 leftVect)
        {
            Vector2 rightVect = new Vector2();


            rightVect.X = vect.X + (vect.X - leftVect.X);
            rightVect.Y = vect.Y + (vect.Y - leftVect.Y);

            return rightVect;
        }




        private void complate_Click(object sender, RoutedEventArgs e)
        {
            isComplate = true;


        }

        /// <summary>
        /// 拖动控制控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
        {
            Thumb thumb = sender as Thumb;


            Vector2 lv = new Vector2();
            switch (thumb.Name)
            {
                case "leftThumb":



                    if (selectPoint == listBessel[0])
                    {
                        Bessel last = listBessel[listBessel.Count - 1];

                        lv.X = (float)(last.LeftVect.X + e.HorizontalChange);
                        lv.Y = (float)(last.LeftVect.Y + e.VerticalChange);

                        last.LeftVect = lv;
                        SetThumbPoint(leftThumb, lv);
                        setLinePoint(leftLine, lv, selectPoint.Vect);

                        canvas.Invalidate();
                    }
                    else
                    {
                        lv.X = (float)(selectPoint.LeftVect.X + e.HorizontalChange);
                        lv.Y = (float)(selectPoint.LeftVect.Y + e.VerticalChange);

                        selectPoint.LeftVect = lv;
                        selectPoint.RightVect = CalculationRightPoint(selectPoint.Vect, selectPoint.LeftVect);

                        setLinePoint(leftLine, lv, selectPoint.Vect);
                        setLinePoint(rightLine, selectPoint.Vect, selectPoint.RightVect);
                        SetThumbPoint(leftThumb, lv);
                        SetThumbPoint(rightThumb, selectPoint.RightVect);
                        canvas.Invalidate();
                    }
                    


                    break;

                case "rightThumb":
                    lv.X = (float)(selectPoint.RightVect.X + e.HorizontalChange);
                    lv.Y = (float)(selectPoint.RightVect.Y + e.VerticalChange);

                    selectPoint.RightVect = lv;

                    if (!(selectPoint == listBessel[0]))
                    {
                        selectPoint.LeftVect = CalculationLeftPoint(selectPoint.Vect, selectPoint.RightVect);

                        setLinePoint(leftLine, selectPoint.LeftVect, selectPoint.Vect);
                        SetThumbPoint(leftThumb, selectPoint.LeftVect);

                    }


                    setLinePoint(rightLine, selectPoint.Vect, selectPoint.RightVect);



                    SetThumbPoint(rightThumb, selectPoint.RightVect);
                    canvas.Invalidate();

                    break;

                case "middThumb":

                    if (selectPoint == listBessel[0] && isClosure)
                    {
                        lv.X = (float)(selectPoint.LeftVect.X + e.HorizontalChange);
                        lv.Y = (float)(selectPoint.LeftVect.Y + e.VerticalChange);

                        Vector2 v = new Vector2();
                        Vector2 rv = new Vector2();

                        rv.X = (float)(selectPoint.RightVect.X + e.HorizontalChange);
                        rv.Y = (float)(selectPoint.RightVect.Y + e.VerticalChange);

                        v.X = (float)(selectPoint.Vect.X + e.HorizontalChange);
                        v.Y = (float)(selectPoint.Vect.Y + e.VerticalChange);

                        selectPoint.LeftVect = lv;
                        selectPoint.RightVect = rv;
                        selectPoint.Vect = v;

                        Bessel last = listBessel[listBessel.Count - 1];

                        last.Vect = new Vector2(last.Vect.X + (float)e.HorizontalChange, last.Vect.Y + (float)e.VerticalChange);
                        last.LeftVect = new Vector2(last.LeftVect.X + (float)e.HorizontalChange, last.LeftVect.Y + (float)e.VerticalChange);
                        last.RightVect = new Vector2(last.RightVect.X + (float)e.HorizontalChange, last.RightVect.Y + (float)e.VerticalChange);



                        setLinePoint(leftLine, last.LeftVect, last.Vect);
                        SetThumbPoint(leftThumb, last.LeftVect);

                        SetThumbPoint(middThumb, selectPoint.Vect);


                        setLinePoint(rightLine, selectPoint.Vect, selectPoint.RightVect);
                        SetThumbPoint(rightThumb, selectPoint.RightVect);
                        canvas.Invalidate();
                    }
                    else
                    {
                        lv.X = (float)(selectPoint.LeftVect.X + e.HorizontalChange);
                        lv.Y = (float)(selectPoint.LeftVect.Y + e.VerticalChange);

                        Vector2 v = new Vector2();
                        Vector2 rv = new Vector2();

                        rv.X = (float)(selectPoint.RightVect.X + e.HorizontalChange);
                        rv.Y = (float)(selectPoint.RightVect.Y + e.VerticalChange);

                        v.X = (float)(selectPoint.Vect.X + e.HorizontalChange);
                        v.Y = (float)(selectPoint.Vect.Y + e.VerticalChange);

                        selectPoint.LeftVect = lv;
                        selectPoint.RightVect = rv;
                        selectPoint.Vect = v;


                        setLinePoint(leftLine, selectPoint.LeftVect, selectPoint.Vect);
                        setLinePoint(rightLine, selectPoint.Vect, selectPoint.RightVect);
                        SetThumbPoint(leftThumb, selectPoint.LeftVect);
                        SetThumbPoint(rightThumb, selectPoint.RightVect);
                        SetThumbPoint(middThumb, selectPoint.Vect);
                        canvas.Invalidate();
                    }



                    break;


            }

        }


        void SetThumbPoint(Thumb thumb, Vector2 point)
        {
            thumb.Translation = new Vector3(point.X - 5, point.Y - 5, 0);

        }

        void setLinePoint(Line line, Vector2 v1, Vector2 v2)
        {
            line.X1 = v1.X;
            line.Y1 = v1.Y;

            line.X2 = v2.X;
            line.Y2 = v2.Y;
        }

       

        private void Thumb_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            HorizontalAndVertical();
        }


        /// <summary>
        /// 显示拖动控件
        /// </summary>
        void VisibleDragThumb(Vector2 lv, Vector2 v, Vector2 rv, bool ls, bool rs)
        {

            SetThumbPoint(leftThumb, lv);
            SetThumbPoint(middThumb, v);
            SetThumbPoint(rightThumb, rv);

            setLinePoint(leftLine, lv, v);
            setLinePoint(rightLine, v, rv);

            leftThumb.Visibility = ls ? Visibility.Visible : Visibility.Collapsed;
            rightThumb.Visibility = rs ? Visibility.Visible : Visibility.Collapsed;


            rightLine.Visibility = leftLine.Visibility = middThumb.Visibility = Visibility.Visible;
        }

  

    
        void CollapsedDragThumb()
        {
            rightLine.Visibility = leftLine.Visibility = leftThumb.Visibility = middThumb.Visibility = rightThumb.Visibility = Visibility.Collapsed;
        }




    
       

        private void vh_Click(object sender, RoutedEventArgs e)
        {
            HorizontalAndVertical();
        }

       
    }


    public class Side
    {
        public List<DeformSide> BesselList { get; set; } = new List<DeformSide>();

       public double Length { get; set; }

    }

}
